# -*- coding:utf8 -*-
__author__ = 'Forcs'

from abc import abstractmethod
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse
from wancake import config
from wancake.resmng.models import Member

class RenderBase(object):

    def __init__(self, view_name, session_id=None):
        self._session_id = session_id
        self._view_name = view_name

    def get_session_id(self):
        """
        :return: 当前用户的session_id
        """
        return self._session_id

    def get_view_name(self):
        """
        :return: 渲染的html
        """
        return self._view_name

    def render(self, request, context=None):
        context = self.render_view_context(request, context)
        return render_to_response(config.BASE_PATH + self._view_name, context)

    @abstractmethod
    def render_view_context(self, request, context=None):
        pass


class DashboardRender(RenderBase):

    def __init__(self, view_name, session_id=None):
        super(DashboardRender, self).__init__(view_name, session_id)

    def render_view_context(self, request, context=None):
        if not context:
            context = {}

        template = "{}?session={}"
        session_id = self.get_session_id()

        hrefs = dict()

        from wancake.auth.main import auth
        from wancake.resmng.main import resmng
        from wancake.weixin.main import weixin
        from wancake import views
        def_navs = [
            {
                "name": "产品详情",
                "view_func": views.product_details,
                "auth_group": "*",
                "auth_rule": "*"
            },
            {
                "name": "订购记录",
                "view_func": views.order_record,
                "auth_group": "30|31",
                "auth_rule": "*"
            },
            {
                "name": "微信相册统计",
                "view_func": weixin.index,
                "auth_group": "30",
                "auth_rule": "*"
            },
            {
                "name": "资源管理",
                "view_func": resmng.index,
                "auth_group": "30",
                "auth_rule": "*",
            },
            {
                "name": "EPG数据同步",
                "view_func": views.epg_syncer,
                "auth_group": "30",
                "auth_rule": "*"
            }
        ]
        hrefs["index"] = template.format(reverse(views.index), session_id)
        hrefs["logout"] = template.format(reverse(auth.logout), session_id)

        nav = list()
        hrefs["nav"] = nav

        try:
            member = Member.objects.get(session_id=session_id)
            group_id = member.group_id
            rule_id = member.rule_id
            nav_hrefs = list()
            for nav_href in def_navs:
                has_group_auth = False
                has_rule_auth = False

                auth_group = nav_href["auth_group"]
                if auth_group == "*":
                    has_group_auth = True
                elif hasattr(auth_group, "split"):
                    auths = auth_group.split("|")
                    for auth_item in auths:
                        if str(group_id) == auth_item:
                            has_group_auth = True
                            break

                auth_rule = nav_href["auth_rule"]
                if auth_rule == "*":
                    has_rule_auth = True
                elif hasattr(auth_rule, "split"):
                    auths = auth_rule.split("|")
                    for auth_item in auths:
                        if str(rule_id) == auth_item:
                            has_rule_auth = True
                            break

                if has_group_auth and has_rule_auth:
                    nav_hrefs.append(nav_href)

        except Member.DoesNotExist:
            nav_hrefs = def_navs

        for nav_href in nav_hrefs:
            item = {
                "href": template.format(reverse(nav_href["view_func"]), session_id),
                "name": nav_href["name"]
            }
            nav.append(item)

        context["hrefs"] = hrefs

        return context
